package com.ugoodtech.mdcc.core.utils;

//import com.sun.image.codec.jpeg.JPEGCodec;
//import com.sun.image.codec.jpeg.JPEGImageEncoder;

//import com.sun.image.codec.jpeg.JPEGCodec;
//import com.sun.image.codec.jpeg.JPEGImageEncoder;

import java.io.*;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-9-10
 * Time: 下午4:54
 * To change this template use File | Settings | File Templates.
 */
public class FileUtil {

    private static File file = null; // 文件对象
//    private String inputDir; // 输入图路径
//    private String outputDir; // 输出图路径
//    private String inputFileName; // 输入图文件名
//    private String outputFileName; // 输出图文件名
//    private int outputWidth = 100; // 默认输出图片宽
//    private int outputHeight = 100; // 默认输出图片高
//    private boolean proportion = true; // 是否等比缩放标记(默认为等比缩放)

    /**
     * @param url  图片地址
     * @param type 图片类型
     * @return
     * @throws java.io.IOException
     */
    public static byte[] fileToByte(String url, String type) throws Exception {
        File file = new File(url);
        FileInputStream fis = new FileInputStream(file);
        byte[] b;
        b = new byte[fis.available()];
        fis.read(b);
        return b;
    }

    public static boolean createFile(File file) throws IOException {
        if (!file.exists()) {
            makeDir(file.getParentFile());
        }
        return file.createNewFile();
    }

    public static void makeDir(File dir) {
        if (!dir.getParentFile().exists()) {
            makeDir(dir.getParentFile());
        }
        dir.mkdir();
    }

    //新建一个文件夹  
    public static String newFolder(String folderPath) {
        String filePath = folderPath;
        try {
            File myFilePath = new File(filePath);
            if (!myFilePath.exists()) {
                myFilePath.mkdir();
            }
        } catch (Exception e) {
            System.out.println("新建文件夹操作出错");
            e.printStackTrace();
        }
        return filePath;
    }

    public static int getRandom() {
        int rand = 3;
        Random random = new Random();
        for (boolean flag = false; flag == false; ) {
            rand = random.nextInt(3) + 3;
            if (rand > 2 && rand < 6) {
                flag = true;
            }
        }
        return rand;
    }
//    public static void main(String[] args)  {
//
//        String imageUrl = "/webapp/resources/images/icon_shop_04.png";
//
//        try {
//            byte[] bytes = imageToByte(imageUrl,"png");
//            System.out.print(bytes.length);
//        } catch (Exception e) {
//            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//        }
//    }


//
//
//    /*
//             * 获得图片大小
//             * 传入参数 String path ：图片路径
//             */
//    public static long getPicSize(String path) {
//        file = new File(path);
//        return file.length();
//    }

    /**
     * 将一个字符串转化为输入流
     */
    public static InputStream getStringStream(String sInputString) {
        if (sInputString != null && !sInputString.trim().equals("")) {
            try {
                ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
                return tInputStringStream;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    public static void saveFileToDir(InputStream in, String filePath) {
//        InputStream in = FileUtil.getStringStream(photo);
        try {
            FileOutputStream out = new FileOutputStream(filePath);
            int c;
            byte buffer[] = new byte[0];
            buffer = new byte[in.available()];
            while ((c = in.read(buffer)) != -1) {
                for (int j = 0; j < c; j++)
                    out.write(buffer);
            }
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
//            throw new MpException("图片上传失败。");
        }
    }

    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
//        System.out.println(filename);
        return filename;
    }

//    main测试
//    compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
//    public static void main(String[] arg) throws IOException {
//        System.out.println("输入的图片大小：" + getPicSize("e:\\icon_shop_02.png")/1024 + "KB");
//        compressPic("e:\\", "e:\\test\\", "icon_shop_02.png", "r1.png", 120, 120, false);
//        System.out.println("输出的图片大小：" + getPicSize("e:\\test\\r1.png")/1024 + "KB");
//        String f = "e:\\test\\";
//        String filePath = FileUtil.newFolder(f); //创建文件夹
//        String imageUrl = "e:\\icon_shop_02.png";
//        File file2 = new File(imageUrl);
//        int tempchar = 0;
//        try {
//            FileOutputStream fos = new FileOutputStream("ddddd");
////            fos.write();
////            FileInputStream fis = new FileInputStream("EEEEE");
////            Reader reader = new InputStreamReader(new FileInputStream("eeeeeeeee"));
////            while ((tempchar = reader.read()) != -1) {
//                // 对于windows下，\r\n这两个字符在一起时，表示一个换行。
//                // 但如果这两个字符分开显示时，会换两次行。
//                // 因此，屏蔽掉\r，或者屏蔽\n。否则，将会多出很多空行。
////                if (((char) tempchar) != '\r') {
////                    System.out.print((char) tempchar);
////                }
////            }
////            reader.close();
////            fis.read();
////            BufferedImage srcImage;
////            srcImage = ImageIO.read(fis);
////            ImageIO.write(srcImage, "png",new File(filePath + "1"));
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//
//    }
}